home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
nild.exe
/
NUMINP.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-29
|
4KB
|
161 lines
// Copyright (C) 1992 James H. Price, All rights reserved
//
// NUMINP.CPP
//
// Member functions for NumInputLine class
//
#define Uses_TEvent
#define Uses_TInputLine
#define Uses_TKeys
#include <tv.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "numinp.h"
template <class Type>
void NumInputLine<Type>::NumInputLine( const TRect& r, int maxLen,
char aMaskChar ) : TInputLine( r, maxLen ), maskChar( aMaskChar )
{
}
template <class Type>
void NumInputLine<Type>::handleEvent( TEvent& event )
{
if( event.what == evKeyDown )
{
ushort keyCode = event.keyDown.keyCode;
switch( keyCode )
{
case kbShiftTab: // save these for TInputLine
case kbTab:
case kbBack:
case kbEnter:
case kbEsc:
break;
default:
if( event.keyDown.charScan.charCode ) // don't handle extended keys
{
if( !isValidChar( event ) ) // check valid characters
clearEvent( event ); // if not valid, ignore
}
}
}
TInputLine::handleEvent( event );
}
template <class Type>
Boolean NumInputLine<Type>::isValidChar( TEvent& event )
{
uchar key = event.keyDown.charScan.charCode;
switch( maskChar )
{
case 'u': // unsigned int or long
if( !isdigit( key ) )
return False;
break;
case 'd': // signed int or long
if( key == '-' )
{
if( curPos && curPos == selEnd ) // accept '-' for initial keystroke
return True;
if ( curPos > 0 ) // don't accept past first position
return False;
if( *data == '-' ) // don't take more than one '-'
return False;
}
else if( !isdigit( key ) )
return False;
break;
case 'f': // signed double or float
if( key == '-' )
{
if( curPos && curPos == selEnd ) // accept '-' for initial keystroke
return True;
if ( curPos > 0 ) // don't accept past first position
return False;
if( *data == '-' ) // don't take more than one '-'
return False;
}
else if( key == '.' )
{
if( curPos && curPos == selEnd ) // accept '.' for initial keystroke
return True;
if( strchr( data, '.' ) ) // don't take more than one '.'
return False;
}
else if( !isdigit( key ) )
return False;
break;
default:
break;
}
return True;
}
template <class Type>
void NumInputLine<Type>::setData( void *rec )
{
numToStr( (Type *)rec, data );
}
template <class Type>
void NumInputLine<Type>::getData( void *rec )
{
Type val = strToNum( data );
memmove( rec, &val, dataSize() );
}
////////////////////////////////////////////////////////////////////////
/////////////////////////// IntInputLine ///////////////////////////////
////////////////////////////////////////////////////////////////////////
IntInputLine::IntInputLine( const TRect& r, int maxLen, char aMaskChar )
: NumInputLine<int>( r, maxLen, aMaskChar )
{
}
void IntInputLine::numToStr( int *rec, char *buf )
{
itoa( *rec, buf, 10 ); // no bounds or overflow checking!!
}
int IntInputLine::strToNum( char *s )
{
return atoi( s );
}
ushort IntInputLine::dataSize()
{
return sizeof( int );
}
////////////////////////////////////////////////////////////////////////
/////////////////////// DoubleInputLine ////////////////////////////////
////////////////////////////////////////////////////////////////////////
DoubleInputLine::DoubleInputLine( const TRect& r, int maxLen,
char aMaskChar ) : NumInputLine<double>( r, maxLen, aMaskChar )
{
}
void DoubleInputLine::numToStr( double *rec, char *buf )
{
gcvt( *rec, maxLen-3, buf ); // Save space for '-', '.', and '\0'
} // otherwise, no overflow or bounds check
double DoubleInputLine::strToNum( char *s )
{
return atof( s );
}
ushort DoubleInputLine::dataSize()
{
return sizeof( double );
}